package com.example.newscrolling.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.newscrolling.enity.Movie;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class UserDAO {
	private static String DB_NAME = "M_list.db";
	private static String SQL = null;
	private static final String TAG = "SQLITE";
	private Context ctx;

	public UserDAO(Context ctx) {
		super();
		this.ctx = ctx;
	}

	public UserDAO() {
		// TODO Auto-generated constructor stub
		super();
	}

	// copy Database
	private void copyDatabase(File dbFile) throws IOException {
		InputStream is = ctx.getAssets().open(DB_NAME);
		OutputStream os = new FileOutputStream(dbFile);

		byte[] buffer = new byte[1024];
		while (is.read(buffer) > 0) {
			os.write(buffer);
		}

		os.flush();
		os.close();
		is.close();
	}

	//開啟資料庫可讀取
	public SQLiteDatabase openReadDatabase() {
		File dbFile = ctx.getDatabasePath(DB_NAME);	
		if (!dbFile.exists()) {
			try {
				File parentDir = new File(dbFile.getParent());
				if (!parentDir.exists()) {
					parentDir.mkdir();
				}
				copyDatabase(dbFile);
			} catch (IOException e) {
				throw new RuntimeException("Error creating source database", e);
			}
		}
		return SQLiteDatabase.openDatabase(dbFile.getPath(), null,SQLiteDatabase.OPEN_READONLY);
	}

	// 讀取資料庫內容放到ArrayList
	public List<Movie>getAll(int position) {
		List<Movie> ret = new ArrayList<Movie>();		
		//String SQL = "SELECT * FROM M_LIST;";	
		switch (position){
		case 0:
			DB_NAME = "M_list.db";
			SQL = "SELECT * FROM M_LIST;";
			break;
		case 1:
			DB_NAME = "M_list2.db";
			SQL = "SELECT * FROM M_LIST2;";
			break;
		case 2:
			DB_NAME = "M_list3.db";
			SQL = "SELECT * FROM M_LIST3;";
			break;	
		}	
		SQLiteDatabase db = this.openReadDatabase();	
		Cursor cursor = db.rawQuery(SQL, null);		
		if (cursor.moveToFirst()) {
			do {
				int id = cursor.getInt(0);
				String time = cursor.getString(1);
				String name = cursor.getString(2);
				byte[] image = cursor.getBlob(3);
				Movie u = new Movie(id, time, name, image);
				ret.add(u);
			} while (cursor.moveToNext());
		}
		cursor.close();		
		db.close();		
		return ret;
	}
	
	public ArrayList<HashMap> getAllm(int selected) {
		ArrayList<HashMap> list = new ArrayList<HashMap>();
		switch (selected) {
		case 0:
			DB_NAME = "M_list.db";
			SQL = "SELECT * FROM M_LIST;";
			break;
		case 1:
			DB_NAME = "M_list2.db";
			SQL = "SELECT * FROM M_LIST2;";
			break;
		case 2:
			DB_NAME = "M_list3.db";
			SQL = "SELECT * FROM M_LIST3;";
			break;
		}
		SQLiteDatabase db = this.openReadDatabase();
		Cursor cursor = db.rawQuery(SQL, null);
		HashMap item = null;
		if (cursor.moveToFirst()) {
			do {
				int id = cursor.getInt(0);
				String time = cursor.getString(1);
				String name = cursor.getString(2);
				byte[] image = cursor.getBlob(3);
				//Movie u = new Movie(id, time, name, image);
				item = new HashMap();
				item.put("name", name);
				item.put("time", time);
				item.put("image", image);
				list.add(item);
			} while (cursor.moveToNext());
		}
		cursor.close();
		db.close();
		return list;
	}
	public Movie getUserById1(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		String sql = "SELECT * FROM USERS;";
		Cursor cursor = db.rawQuery(sql, null);
		Movie ret = null;
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(position);
			
		int id = cursor.getInt(0);
		    String time = cursor.getString(1);
			String name = cursor.getString(2);
			byte[] image = cursor.getBlob(3);
			ret = new Movie(id, time, name,image);
		}
		return ret;
	}

	public Bitmap getBmp(byte[] image) {
		// TODO Auto-generated method stub
		Bitmap bmpOut = null;
		byte[] dataImage = image;
		bmpOut = BitmapFactory.decodeByteArray(dataImage, 0, dataImage.length);
		return bmpOut;
		
	}



}
